<template>
  <div>
    <van-nav-bar title="黑马头条-登录" fixed placeholder />
    <van-form @submit="onSubmit" v-model='form'>
      <van-field
        v-model="form.mobile"
        required
        name:mobile
        safe-area-inset-top
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true,pattern:/^1[3-9]\d{9}$/, message: '请填手机号'}]"
      />
      <van-field
        v-model="form.code"
        type="password"
        name:code
        required
        label="验证码"
        placeholder="验证码"
        :rules="[{ required: true,pattern:/^\d{6}$/, message: '请填写验证码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" :loading="isLogin" loading-text="加载中..." :disabled="isLogin" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token'
import { Notify } from 'vant'
export default {

  data () {
    return {
      form: {
        mobile: '13888888888',
        code: '246810'

      },
      isLogin: false

    }
  },
  methods: {
    async onSubmit (values) {
      try {
        this.isLogin = true
        const res = await loginAPI({ ...this.form })
        console.log(res)
        setToken(res.data.data.token)
        Notify({ type: 'success', message: '登陆成功' })
        this.isLogin = false
        this.$router.push('/layout')
      // console.log('submit', values)
      } catch (error) {
        Notify({ type: 'danger', message: '登陆失败' })
        console.error('错了')
      }
    }
  }
}
</script>

<style lang = "less" scoped>
</style>
